package com.sam.im.heiqun.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Joe on 2017/1/13.
 */

public class DateFormatUtil {

    /**
     * 时间戳转日期
     *
     * @param ms
     * @return
     */
    public static String transForDate(long ms) {
        if ((int) ms == 0)
            return "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = sdf.format(ms);
        long last = System.currentTimeMillis() - ms;
        if (last > 0) {
            if (last < 60 * 60 * 1000) {
                String lastStr = str.substring(11, 16);
                return lastStr;
            } else if (last >= 60 * 60 * 1000 && last < 60 * 60 * 1000 * 24) {
                return (int) (last / (60 * 60 * 1000)) + "小时";
            } else {
                return str.substring(5, 10);
            }
        } else {
            return str.substring(14, 19);
        }
    }

    public static String transferLastTime(long date) {

        String result = "";
        long DAY_UNIT = 1000 * 60 * 60 * 24;
        long day = date / DAY_UNIT;
        if (day > 0) {
            result = result + day + "天";
        }

        long HOUR_UNIT = 1000 * 60 * 60;
        long hour = (date - DAY_UNIT * day) / HOUR_UNIT;
        if (hour > 0) {
            result = result + hour + "小时";
        }

        long MINUTE_UNIT = 1000 * 60;
        long minute = (date - DAY_UNIT * day - HOUR_UNIT * hour) / MINUTE_UNIT;
        if (minute > 0) {
            result = result + minute + "分钟";
        }

        long SECOND_UNIT = 1000;
        long second = (date - DAY_UNIT * day - HOUR_UNIT * hour - MINUTE_UNIT * minute) / SECOND_UNIT;
        if (second > 0) {
            result = result + second + "秒";
        }
        return result;
    }

    /*
         * 将时间戳转换为时间
         */
    public static String stampToDate(long s){
        String res;
       // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
    public static String transferLastTime1(long date) {

        String result = "";
        long DAY_UNIT = 1000 * 60 * 60 * 24;
        long day = date / DAY_UNIT;
        if (day > 0) {
            result = result + day + "天";
        }

        long HOUR_UNIT = 1000 * 60 * 60;
        long hour = (date - DAY_UNIT * day) / HOUR_UNIT;
        if (hour > 0) {
            result = result + hour + "小时";
        }

        long MINUTE_UNIT = 1000 * 60;
        long minute = (date - DAY_UNIT * day - HOUR_UNIT * hour) / MINUTE_UNIT;
        if (minute > 0) {
            result = result + minute + "分钟";
        }
        return result;
    }

    public static String tomm_ss(long date){

        return format(date,"mm:ss");
    }

    public static String format(long date,String format){

        return new SimpleDateFormat(format, Locale.CHINESE).format(date);
    }
}
